home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8451 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: s02.pavilion.co.uk!usenet
  2. From: AJRobb@pavilion.co.uk (Andy J Robb)
  3. Newsgroups: comp.lang.c,comp.lang.c++,comp.os.ms-windows.programmer.misc,comp.os.msdos.programmer,comp.programming,comp.windows.ms.programmer
  4. Subject: Re: Date Arithmetic
  5. Date: Sun, 18 Feb 1996 17:58:04 GMT
  6. Organization: Pavilion Internet plc
  7. Message-ID: <4g7p9r$384@s02.pavilion.co.uk>
  8. References: <4g19kp$640@tracy.protocom.com> <3124A458.7BF8@f25mail.amd.com>
  9. NNTP-Posting-Host: poolb05.pavilion.co.uk
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Clark Archer <carcher@f25mail.amd.com> wrote:
  13.  
  14. >Michael J. Karas wrote:
  15. >> 
  16. >> I am working on an algorithm for a laser marking machine that writes
  17. >> expiration delays on to food product boxes. The algorithm needs to
  18. >> be able to add NNN days to todays date in the fastest manner possible
  19. >> without using any floating point arithmetic. I could use help from anyone
  20. >> that has C code for doing this. It would be nice if the solution took the
  21. >> leap year problem in to account including the special case of the year
  22. >> 2000. Thanks in advance to anyone who could share their knowledge on this
  23. >> subject.
  24. >> -----
  25. >> Michael Karas             | EMail: mkaras@pclink.com
  26. >> Carousel Design Solutions | America OnLine: MJKaras
  27. >> 6021 Logan Avenue South   | Voice: (612) 861-1284
  28. >> Minneapolis MN 55419      | Fax:   (612) 861-1386
  29. >> -----
  30.  
  31. >You could use the C runtime library function time() to get the current 
  32. >time and then add NNN * SECS_PER_DAY to it and then convert back to a 
  33. >struct tm like:
  34. >    
  35. >    #define SECS_PER_DAY  86400
  36.  
  37. >    time_t tmtNow;
  38. >    struct tm tmThen;
  39.  
  40. >    tmtNow = time(NULL);
  41. >    tmtNow += (nDaysToAdd * SECS_PER_DAY);
  42. >    tmThen = *localtime(tmtNow);
  43.  
  44. This requires an implementation where time_t is seconds.
  45. ISO/ANSI only requires an arithmetic type, e.g. using packed bitfields
  46. is legal - like MS-DOS.  To be truly portable, you might want to go
  47. back to the Gaus algorithm (e.g. used in Linux kernel).  If you want I
  48. can dig it out for you.  This uses a handful of integer multiplies and
  49. divides and copes with all leap years.
  50.  
  51. Regards,
  52. Andy Robb.
  53. -----BEGIN PGP PUBLIC KEY BLOCK-----
  54. Version: 2.6.2i
  55.  
  56. mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
  57. MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
  58. B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
  59. tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
  60. IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
  61. =/wVD
  62. -----END PGP PUBLIC KEY BLOCK-----
  63.  
  64.